using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

using Microsoft.DirectX.Direct3D;

namespace iDorm
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Panel target;
		private Renderer r;

		private bool mousedown = false;

		int mouseX, mouseY;


		// Room
		Room room;
		private System.Windows.Forms.ListBox listBox1;
		private System.Windows.Forms.Button button1;

		// game Mode
		private bool in3D;
		private System.Windows.Forms.MainMenu mainMenu1;
		private System.Windows.Forms.MenuItem menuItem1;
		private System.Windows.Forms.MenuItem menuItem2;
		private System.Windows.Forms.MenuItem menuItem3;
		private System.Windows.Forms.MenuItem menuItem4;
		private System.Windows.Forms.MenuItem menuItem5;
		private System.Windows.Forms.MenuItem menuItem6;
		private System.Windows.Forms.MenuItem menuItem7;
		private System.Windows.Forms.MenuItem menuItem8;
		private System.Windows.Forms.SaveFileDialog SaveDialog;

		/// <summarry>
		/// Required Properties
		/// </summarry>
		private bool AppStillIdle
		{
			get
			{
				NativeMethods.Message msg;
				return !NativeMethods.PeekMessage(out msg, IntPtr.Zero, 0, 0, 0);
			}
		}
		
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public Form1()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.target = new System.Windows.Forms.Panel();
			this.listBox1 = new System.Windows.Forms.ListBox();
			this.button1 = new System.Windows.Forms.Button();
			this.mainMenu1 = new System.Windows.Forms.MainMenu();
			this.menuItem1 = new System.Windows.Forms.MenuItem();
			this.menuItem3 = new System.Windows.Forms.MenuItem();
			this.menuItem4 = new System.Windows.Forms.MenuItem();
			this.menuItem5 = new System.Windows.Forms.MenuItem();
			this.menuItem6 = new System.Windows.Forms.MenuItem();
			this.menuItem7 = new System.Windows.Forms.MenuItem();
			this.menuItem2 = new System.Windows.Forms.MenuItem();
			this.menuItem8 = new System.Windows.Forms.MenuItem();
			this.SaveDialog = new System.Windows.Forms.SaveFileDialog();
			this.SuspendLayout();
			// 
			// target
			// 
			this.target.BackColor = System.Drawing.SystemColors.Desktop;
			this.target.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
			this.target.Location = new System.Drawing.Point(8, 8);
			this.target.Name = "target";
			this.target.Size = new System.Drawing.Size(640, 480);
			this.target.TabIndex = 0;
			this.target.MouseUp += new System.Windows.Forms.MouseEventHandler(this.target_MouseUp);
			this.target.MouseMove += new System.Windows.Forms.MouseEventHandler(this.target_MouseMove);
			this.target.MouseDown += new System.Windows.Forms.MouseEventHandler(this.target_MouseDown);
			// 
			// listBox1
			// 
			this.listBox1.Items.AddRange(new object[] {
														  "Bed",
														  "Chair",
														  "Desk"});
			this.listBox1.Location = new System.Drawing.Point(656, 32);
			this.listBox1.Name = "listBox1";
			this.listBox1.Size = new System.Drawing.Size(184, 160);
			this.listBox1.TabIndex = 1;
			// 
			// button1
			// 
			this.button1.Location = new System.Drawing.Point(664, 240);
			this.button1.Name = "button1";
			this.button1.Size = new System.Drawing.Size(176, 32);
			this.button1.TabIndex = 2;
			this.button1.Text = "Add";
			this.button1.Click += new System.EventHandler(this.button1_Click);
			// 
			// mainMenu1
			// 
			this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					  this.menuItem1,
																					  this.menuItem2});
			// 
			// menuItem1
			// 
			this.menuItem1.Index = 0;
			this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					  this.menuItem3,
																					  this.menuItem4,
																					  this.menuItem5,
																					  this.menuItem6,
																					  this.menuItem7});
			this.menuItem1.Text = "File";
			// 
			// menuItem3
			// 
			this.menuItem3.Index = 0;
			this.menuItem3.Text = "New";
			// 
			// menuItem4
			// 
			this.menuItem4.Index = 1;
			this.menuItem4.Text = "Save";
			this.menuItem4.Click += new System.EventHandler(this.menuItem4_Click);
			// 
			// menuItem5
			// 
			this.menuItem5.Index = 2;
			this.menuItem5.Text = "Open";
			// 
			// menuItem6
			// 
			this.menuItem6.Index = 3;
			this.menuItem6.Text = "-";
			// 
			// menuItem7
			// 
			this.menuItem7.Index = 4;
			this.menuItem7.Text = "Exit";
			// 
			// menuItem2
			// 
			this.menuItem2.Index = 1;
			this.menuItem2.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					  this.menuItem8});
			this.menuItem2.Text = "Help";
			// 
			// menuItem8
			// 
			this.menuItem8.Index = 0;
			this.menuItem8.Text = "Uhhh...";
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(848, 589);
			this.Controls.Add(this.button1);
			this.Controls.Add(this.listBox1);
			this.Controls.Add(this.target);
			this.Menu = this.mainMenu1;
			this.Name = "Form1";
			this.Text = "iDorm";
			this.Load += new System.EventHandler(this.Form1_Load);
			this.ResumeLayout(false);

		}
		#endregion



		// onAppIdle
		//
		// Description:	Called every time the application has a free second
		// Input:		Object - not important
		//				Event args - not used
		// Output:		None
		public void onAppIdle(object sender, EventArgs e)
		{

			while (AppStillIdle)
			{
				if (!in3D)
				{
					r.BeginRender();
					{
						// This is where all frame by frame processing should occur
							
						room.RenderRoom2D(r.GetDevice());
					}
					r.EndRender();	
				}
			}
		}


		private void Form1_Load(object sender, System.EventArgs e)
		{
			r = new Renderer(target);
			room = new Room(12, 9);

			in3D = false;

		}

		private void button1_Click(object sender, System.EventArgs e)
		{
			Object obj;
			if (listBox1.SelectedIndex == 0)
			{
				obj = new Object( 0,0,200,100,3);
			}
			else if (listBox1.SelectedIndex == 1)
			{
				obj = new Object(0,0, 50, 50, 2);
			}
			else
			{
				obj = new Object(0,0, 200, 100, 3);
			}
			room.AddObject(obj);
		}

		private void target_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
		{
			if (e.Button == System.Windows.Forms.MouseButtons.Left)
			{
				ArrayList objects = room.GetObjects();

				foreach (Object o in objects)
				{
					Point p = o.Intersects(new Point(e.X, e.Y));

					
					if (p.X > 0 && p.Y > 0)	
					{					
						mouseX = p.X;
						mouseY = p.Y;
						o.moving = true;
					}
				}

				mousedown = true;
			}
		}

		private void target_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
		{
			if (e.Button == System.Windows.Forms.MouseButtons.Left)
			{
				ArrayList objects = room.GetObjects();

				foreach (Object o in objects)
				{
					o.moving = false;
					mousedown = false;
				}
			}
		}

		private void target_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
		{
			if (mousedown)
			{
				ArrayList objects = room.GetObjects();

				foreach (Object o in objects)
				{
					if (o.moving)
						o.Move(e.X - mouseX, e.Y - mouseY);
				}
			}
		}

		private void menuItem4_Click(object sender, System.EventArgs e)
		{
			SaveDialog.ShowDialog();
		}

	}
}
